﻿@model IEnumerable<BuildServer.Models.BuildDefinition>

@{
    ViewBag.Title = "Build Definitions";
}

<h2 class="title"><img src="~/Images/BuildDefinition.png" />@ViewBag.Title</h2>

<table class="data-table">
    <thead>
        <tr>
            <th class="data-group"></th>
            @Html.ColumnHeaderFor(model => model.Name)
            @Html.ColumnHeaderFor(model => model.IsNightly)
            @Html.ColumnHeaderFor(model => model.BuildTemplate, false)
            <th>Actions</th>
        </tr>
    </thead>
    @foreach (var group in Model.GroupBy(x => x.BuildTemplate.Name).OrderBy(x => x.Key))
    {
        @GroupHeader(group.Key, 6)
        @GroupContent(group);
    }
</table>

<div>
@Html.ActionLink("Create New", "Create") |
@Html.ActionLink("Schedule Builds", "Index", "Schedule")
</div>

@helper GroupHeader(string groupName, int columns)
{
    groupName = string.IsNullOrWhiteSpace(groupName) ? "[other]" : groupName;
    <tbody class="data-group-header">
        <tr>
            <td colspan="@columns">
                <span class="data-group-expand"><img src="~/Images/CollapsedFilePicker_14x_F12.png" width="10" /> @groupName</span>
                <span class="data-group-collapse"><img src="~/Images/ExpandedFilePicker_14x_F12.png" width="10" /> @groupName</span>
            </td>
        </tr>
    </tbody>
}

@helper GroupContent(IEnumerable<BuildServer.Models.BuildDefinition> items)
{
    <tbody class="data-group-content">
        @foreach (var item in Html.Sort(items))
        {
            <tr class="data-row">
                <td class="data-group"></td>
                <td>
                    @Html.ActionLink(item.Name, "Edit", new { id = item.Id })
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IsNightly, "YesNo")
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.BuildTemplate.Name)
                </td>
                <td class="data-hover" style="width:1px;white-space:nowrap;">
                    <span>
                        @Html.ActionLink("Edit Properties", "Edit", "Properties", new { id = item.Id }, null) |
                        @Html.ActionLink("Copy", "Copy", new { id = item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                    </span>
                </td>
            </tr>
        }
    </tbody>
}

@section Scripts
{
    <script type="text/javascript">
        function toggle_data_group() {
            var groupHeader = $(this).closest('.data-group-header')
            groupHeader.find('.data-group-expand, .data-group-collapse').toggle();
            groupHeader.next('.data-group-content').toggle();
        }
        $(document).ready(function () {
            $('.data-group-expand').show().click(toggle_data_group);
            $('.data-group-collapse').hide().click(toggle_data_group);
            $('.data-group-content').hide();
            $('.data-group-expand:first').click();
        });
    </script>
}